/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */


struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2)
{
    struct ListNode* t1 = l1;
    struct ListNode* t2 = l2;
    struct ListNode* newnode = (struct ListNode*)malloc(sizeof(struct ListNode));
    newnode->next = NULL;
    struct ListNode* pre = newnode;
    int temp=0;
    while(t1 || t2)
    {
        if(t1 !=NULL && t2 == NULL)
        {
            newnode->val = t1->val+temp;
            t1= t1->next;
            temp = 0;
        }
        else if(t1== NULL && t2 !=NULL)
        {
             newnode->val = t2->val+temp;
             t2 = t2->next;
             temp =0;
        }
        if(t1 !=NULL&& t2 != NULL)
        {
            if(t1->val + t2->val >=10)
            {
                if(temp >0)
                {
                    newnode->val = (t1->val + t2->val)%10 + temp;
                    temp =0;
                }
                else
                {
                    newnode->val = (t1->val + t2->val)%10;
                    temp++;
                }
                t1= t1->next;
                t2 = t2->next;
            }
            else
            {
                newnode->val = t2->val+ t1->val +temp;
                t2 = t2->next;
                t1 = t1->next;
                temp = 0;
            }
        }
        if(t1 ||t2)
        {
            newnode->next = (struct ListNode*)malloc(sizeof(struct ListNode));
            newnode = newnode->next;
            newnode ->next = NULL;
        }
    }
    return pre;
}